package com.leetcode;

import ds.ListNode;

/**
 * TODO
 *
 * @author lefly
 * @version 1.0
 * @classname No141
 * @description https://leetcode-cn.com/problems/linked-list-cycle/
 * @date 2021/2/7 下午1:12
 */
public class No141 {
    public static void main(String[] args) {
        ListNode l1 = new ListNode(1);
        ListNode l2 = new ListNode(2);
        ListNode l3 = new ListNode(3);
        ListNode l4 = new ListNode(4);
        ListNode l5 = new ListNode(5);
        l1.next = l2;
        l2.next = l3;
        l3.next = l4;
        l4.next = l5;
        l5.next = l3;
        boolean res = hasCycle(l1);
        System.out.println(res);


    }

    public static boolean hasCycle(ListNode head) {
        if(head == null || head.next == null){
            return false;
        }
        ListNode slow = head,fast = head.next;
        while (fast != null && fast.next != null && slow != null){
            if(slow == fast){
                return true;
            }
            fast = fast.next.next;
            slow = slow.next;
        }
        return false;
    }
}
